package com.jielihaofeng.websocket.controller;

import com.jielihaofeng.websocket.service.WebSocketServer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * @description
 * @author Johnnie Wind
 * @date 2021/1/11 18:20
 */
@Controller
public class WebSocketController {

    @GetMapping("/index")
    public String index() {
        return "index";
    }

    @GetMapping("/webSocket")
    public ModelAndView socket(){
        ModelAndView mav = new ModelAndView("/webSocket");
//        mav.addObject("userId",userId);
        return mav;
    }

    @ResponseBody
    @RequestMapping("/socket/push/{cid}")
    public Map pushToWeb(@PathVariable("cid")String cid,String message){
        Map<String, Object> result = new HashMap<>();
        try {
            WebSocketServer.sendInfo(message,cid);
            result.put("code",cid);
            result.put("msg",message);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

}
